#!/usr/bin/env python
# coding=utf-8
#-------------------------------------------------------------------------------
# Name:        gjsk_getter.py
# Purpose:     get gjsk url
#
# Author:      vavava
#
# Created:     04/11/2012
# Copyright:   (c) vavava 2012
# Licence:     <your licence> reg_str=r"<embed[^>]*src='(?P<link>)[^']*"
#-------------------------------------------------------------------------------

from vavava import httpclient

__author__ = 'vavava'

import   re
from vavava import util

class gjsk(object):

    def __init__(self,debugLevel=0):
        self.http = httpclient.HttpClient(debug_level=debugLevel)
        self.http.EnableCookieSupport()
        self.fetch_url=r'http://vtwtv.info/home.php?mod=space&uid=1278&do=thread&view=me&type=thread&order=dateline&from=space&page=%s'
        self.page_dic={}
        self.video_list=[]
        self.youtube_url_dic={}

    def login(self):
        url = r"http://vtwtv.info/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1"
        param = {
            "fastloginfield":"username",
            "username":"vavava",
            "password":"123123123",
            "quickforward":"yes",
            "handlekey":"ls"
        }
        self.http.Post(url,param)

    def get_page_dic(self,index):
        import vavava.util
        self.login()
        page = self.http.Get(self.fetch_url%index)
        page_text=page.decode('utf-8','ignore')
        reg_str=r'<a[^>]*href="([^"]*)"[^>]*>([^<]*關鍵時刻[^<]*)</a>'
        match=vavava.util.reg_helper( page_text, reg_str, re.I|re.S)
        if match:
            for each in match:
                self.page_dic[each[1]]='http://vtwtv.info/'+each[0].replace('&amp;','&')
            return True
        return False

    def get_youtube_url(self,indexes,log):
        for index in indexes:
            try:
                self.get_page_dic(index)
            except Exception as e:
                log.exception(e)

        for each in self.page_dic:
            try:
                url=self.page_dic[each]
                log.info (url)
                self.login()
                page = self.http.Get(url)
                page_text=page.decode('utf-8','ignore')
                reg_str=r"<param [^\?]*value=[^\?]*/(?P<link>[^\?]*)\?"
                reg=re.compile(reg_str,re.S)
                match=reg.findall(page_text)
                if match:
                    url_format=r'http://www.youtube.com/watch?feature=player_embedded&v=%s'%match[0]
                    self.youtube_url_dic[each]=url_format.replace('&amp;','&')
                else:
                    log.info("not found:"+each)
            except Exception as e:
                log.exception(e)
        return self.youtube_url_dic


if __name__ == '__main__':
    import vavava.util,os
    log = vavava.util.initlog("./log/"+ os.path.split(__file__)[1]+".log")

    g=gjsk()
    indexes=["1","2","3"]
    if g.get_youtube_url(indexes,log):
        for each in g.youtube_url_dic:
            log.info(r'%s       %s'%(each,g.youtube_url_dic.get(each)))
    else:
        log.info("failed")
